function objToString(obj) {

    if (obj == null) {
        return null;
    }

    if (typeof obj === 'object') {
        return JSON.stringify(obj);
    } else {
        return obj;
    }
}

//字符串拼接函数
function concat() {
    var s = "";
    for (var i = 0; i < arguments.length; i++) {
        s += arguments[i]
    }
    return s
}

//替换目标内容为指定内容
function cas(obj, from, to) {
    if (obj === from) {
        return to;
    } else {
        return obj;
    }
}

//利用正则提取内容
function extract(obj, regex) {
    if (!obj) {
        return obj;
    }
    //编译一个正则
    var reg = new RegExp(regex);
    var s = obj.toString();
    var list = s.match(reg);
    if (list && list.length > 0) {
        return list[0];
    } else {
        return null;
    }
}

//某个元素在数组中的位置
function indexOf(arr, obj) {
    return arr.indexOf(obj);
}

//如果未定义_index则表明使用全局自增索引
var _globalIndex = 0;

//根据_index构建索引
function $index(n) {
    if (n == null) {
        if (_index != null) {
            return _index + "";
        } else {
            return (_globalIndex++) + "";
        }
    }
    if (_index != null) {
        return n + _index + "";
    } else {
        return (_globalIndex++) + "";
    }
}


//当参数的长度为0或者为1的时候不调用java缓存
var _blankArgCacheMap = {};

//一个参数
var _oneArgCacheMap = {};

//清空缓存
function _clearCache() {
    _blankArgCacheMap = {};
    _oneArgCacheMap = {};
    _java.clearCache();
}

function _cachePut(args, result) {

    if (args.length === 0) {
        _blankArgCacheMap[args.callee.name] = result;
        return result
    }

    if (args.length === 1) {
        var map = _oneArgCacheMap[args.callee.name];
        if (map == null) {
            map = {};
            _oneArgCacheMap[args.callee.name] = map;
        }
        map[args[0]] = result;
        return result;
    }

    var arg = [];

    for (var i = 0; i < args.callee.length; i++) {
        arg[i] = args[i];
    }
    _java.cachePut(args.callee.name, arg, result);
    return result
}

function _cacheGet(args) {

    if (args.length === 0) {
        return _blankArgCacheMap[args.callee.name]
    }

    if (args.length === 1) {
        var map = _oneArgCacheMap[args.callee.name];
        if (map == null) {
            return null;
        }
        return map[args[0]];
    }

    var arg = [];
    for (var i = 0; i < args.callee.length; i++) {
        arg[i] = args[i];
    }
    return _java.cacheGet(args.callee.name, arg);

}

//需要提供更加高效的缓存机制
//从参数中提取出相应的结果
function reversal(list) {
    var r = _cacheGet(arguments);
    if (r != null) {
        return r;
    }

    if (list == null) {
        return null;
    }

    r = [];

    for (var i = 0; i < list.length; i++) {
        r[i] = list[list.length - 1 - i];
    }

    return _cachePut(arguments, r);

}

function array(len, value) {

    var r = _cacheGet(arguments);
    if (r != null) {
        return r;
    }

    r = [];
    if (!len) {
        return r;
    }

    if (value == null) {
        value = '';
    }

    for (var i = 0; i < len; i++) {
        r[i] = value;
    }

    return _cachePut(arguments, r);

}

//对齐一个空白数组

function align(list, value) {
    var r = _cacheGet(arguments);
    if (r != null) {
        return r;
    }

    if (list == null) {
        return null;
    }
    if (value == null) {
        value = ""
    }
    r = [];
    for (var i = 0; i < list.length; i++) {
        r[i] = value;
    }
    return _cachePut(arguments, r);
}


//将空白的元素填充为给定值
function fill(arr, value) {

    var r = _cacheGet(arguments);
    if (r != null) {
        return r;
    }


    if (arr == null) {
        return arr;
    }

    if (value == null) {
        value = '';
    }

    for (var i = 0; i < arr.length; i++) {
        if (!arr[i]) {
            arr[i] = value;
        }
    }

    return _cachePut(arguments, arr);

}

function change(arr, from, to) {
    var r = _cacheGet(arguments);
    if (r != null) {
        return r;
    }
    if (arr == null) {
        return arr;
    }

    if (from == null) {
        return arr;
    }

    if (to == null) {
        to = '';
    }

    for (var i = 0; i < arr.length; i++) {
        if (arr[i] === from) {
            arr[i] = to;
        }
    }
    return _cachePut(arguments, arr);
}